package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Channel;
import com.ruoyi.system.service.IChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 销售渠道Controller
 *
 * @author ruoyi
 * @date 2022-04-13
 */
@RestController
@RequestMapping("/system/channel")
public class ChannelController extends BaseController {
    @Autowired
    private IChannelService channelService;

    /**
     * 查询销售渠道列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Channel channel) {
        startPage();
        List<Channel> list = channelService.selectChannelList(channel);
        return getDataTable(list);
    }
    /**
     * 获取销售渠道详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(channelService.selectChannelById(id));
    }

    /**
     * 新增销售渠道
     */
    @PostMapping
    public AjaxResult add(@RequestBody Channel channel) {
        Channel res = new Channel();
        res.setName(channel.getName());
        List<Channel> list = channelService.selectChannelList(res);
        Assert.isTrue(list.size()==0,"该销售渠道已有数值");
        channel.setCreateUser(SecurityUtils.getUserId().intValue());
        return toAjax(channelService.insertChannel(channel));
    }

    /**
     * 修改销售渠道
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Channel channel) {
        channel.setUpdateUser(SecurityUtils.getUserId().intValue());
        return toAjax(channelService.updateChannel(channel));
    }

    /**
     * 删除销售渠道
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(channelService.deleteChannelByIds(ids));
    }
}
